#include "player.h"
#include<QGraphicsPixmapItem>
#include<QObject>
#include<QPixmap>
#include"config.h"
#include<QKeyEvent>
#include"enemy.h"
#include"hong.h"
#include"t_cell.h"
#include<jushi.h>
#include<QGraphicsScene>
#include"bullet.h"
#include"health.h"
#include"score.h"
#include<QGraphicsTextItem>
#include<QGraphicsItem>
#include<QFont>
#include<QFontMetrics>
#include<QLineEdit>


Player::Player(QGraphicsItem*parent):QGraphicsPixmapItem(parent)
{
    //玩家图片大小
    setPixmap(QPixmap(":/images/xijun.png"));
    setScale(PS);
    setPos(boundingRect().width()*PS-130,
           SH/2-boundingRect().height()*PS/2);
    setFlag(QGraphicsItem::ItemIsFocusable);
    setFocus();

    startTimer(1000);


}

void Player::keyPressEvent(QKeyEvent *event)
{
    //按键控制
    switch(event->key()){
        //位置移动
    case Qt::Key_Up:
        if(pos().y()>0)
            setPos(x(),y()-5);
        break;
    case Qt::Key_Down:
        if(pos().y()<SH-boundingRect().height()*PS)
            setPos(x(),y()+5);
        break;
        //打开说明
    case Qt::Key_N:
        if(!win) messageItem->hide();
        if(win) messageItemwin->hide();
        N=1;
        if(playing) return;
        explain();
        break;
        //攻击


    case Qt::Key_Space:
       { Bullet *bullet=new Bullet;

        bullet->setPos(x(),y()+boundingRect().height()*PS/2);
        scene()->addItem(bullet);
        break;}
        //选择关卡
    case Qt::Key_1:
        if(playing) return;
        playing=1;
        N=0;
        Health::getInstance().reset();
        Score::getInstance().reset();
        if(!win) messageItem->hide();
        if(win) messageItemwin->hide();
        if(messageItemN)
        {messageItemN->hide();
            messageItemN1->hide();
            messageItemN10->hide();
            messageItemN2->hide();
            messageItemN20->hide();
            messageItemN3->hide();
            messageItemN30->hide();
            messageItemN4->hide();
            messageItemN40->hide();
            messageItemN5->hide();
            messageItemN50->hide();
        }
        win=0;
        break;
    case Qt::Key_2:
        if(playing) return;
        playing=2;
        N=0;

        Health::getInstance().reset();
        Score::getInstance().reset();
        if(!win) messageItem->hide();
        if(win) messageItemwin->hide();
        if(messageItemN)
        {messageItemN->hide();
            messageItemN1->hide();
            messageItemN10->hide();
            messageItemN2->hide();
            messageItemN20->hide();
            messageItemN3->hide();
            messageItemN30->hide();
            messageItemN4->hide();
            messageItemN40->hide();
            messageItemN5->hide();
            messageItemN50->hide();
        }
        win=0;
        break;
    case Qt::Key_3:
        if(playing) return;
        playing=3;
        N=0;

        Health::getInstance().reset();
        Score::getInstance().reset();
        if(!win) messageItem->hide();
        if(win) messageItemwin->hide();
        if(messageItemN)
        {messageItemN->hide();
            messageItemN1->hide();
            messageItemN10->hide();
            messageItemN2->hide();
            messageItemN20->hide();
            messageItemN3->hide();
            messageItemN30->hide();
            messageItemN4->hide();
            messageItemN40->hide();
            messageItemN5->hide();
            messageItemN50->hide();
        }
        win=0;
        break;
    case Qt::Key_4:
        if(playing) return;
        playing=4;
        N=0;
        Health::getInstance().reset();
        Score::getInstance().reset();
        if(!win) messageItem->hide();
        if(win) messageItemwin->hide();
        if(messageItemN)
        {messageItemN->hide();
            messageItemN1->hide();
            messageItemN10->hide();
            messageItemN2->hide();
            messageItemN20->hide();
            messageItemN3->hide();
            messageItemN30->hide();
            messageItemN4->hide();
            messageItemN40->hide();
            messageItemN5->hide();
            messageItemN50->hide();
        }
        win=0;
        break;

    }


}

//产生敌人
void Player::enemySpawn()
{
    if(playing){
        Enemy *enemy=new Enemy;
        scene()->addItem(enemy);

    }

}

void Player::hongSpawn()
{
    if(playing){
        Hong *hong=new Hong;
        scene()->addItem(hong);

    }

}

void Player::jushiSpawn()
{
    if(playing){
        Jushi *jushi=new Jushi;
        scene()->addItem(jushi);

    }

}


void Player::t_cellSpawn()
{
    if(playing){
        T_cell *t_cell=new T_cell;
        scene()->addItem(t_cell);

    }

}
//游戏结束
void Player::gameOver()
{
    playing=0;
    //释放
    for(auto item:scene()->items()){
        if(typeid(*item)==typeid(Enemy)){
            scene()->removeItem(item);
            delete item;
        }

    }

    for(auto item:scene()->items()){
        if(typeid(*item)==typeid(Hong)){
            scene()->removeItem(item);
            delete item;
        }
    }

    for(auto item:scene()->items()){
        if(typeid(*item)==typeid(Jushi)){
            scene()->removeItem(item);
            delete item;
        }

    }

    for(auto item:scene()->items()){
        if(typeid(*item)==typeid(T_cell)){
            scene()->removeItem(item);
            delete item;
        }

    }

    //判断输赢并输出

    if(!N&&!win){

        if(!messageItem){
            messageItem=new QGraphicsTextItem;
            scene()->addItem(messageItem);
            QString message("Game Over!按N打开关卡介绍！按1~4挑选关卡！");
            messageItem->setPlainText(message);
            messageItem->setDefaultTextColor(Qt::black);
            QFont font("Courier New",32,QFont::Bold);
            messageItem->setFont(font);

            QFontMetrics fm(font);
            int msW=fm.horizontalAdvance(message);
            messageItem->setPos(SW/2-msW/2,
                                SH/2);

        }else
            messageItem->show();

    }

    if(!N&&win){

        if(!messageItemwin){
            messageItemwin=new QGraphicsTextItem;
            scene()->addItem(messageItemwin);
            QString messagewin("You Win!按N打开关卡介绍！按1~4挑选关卡！");
            messageItemwin->setPlainText(messagewin);
            messageItemwin->setDefaultTextColor(Qt::black);
            QFont font("Courier New",32,QFont::Bold);
            messageItemwin->setFont(font);

            QFontMetrics fm(font);
            int msW=fm.horizontalAdvance(messagewin);
            messageItemwin->setPos(SW/2-msW/2,
                                SH/2);

        }else
            messageItemwin->show();

    }
}

//关卡说明文档
void Player::explain(){

    if(!messageItemN){
        messageItemN=new QGraphicsTextItem;
        scene()->addItem(messageItemN);
        QString messageN("关卡介绍");
        messageItemN->setPlainText(messageN);
        messageItemN->setDefaultTextColor(Qt::black);
        messageItemN->setFont(QFont("Courier New",32,QFont::Bold));

        messageItemN->setPos(SW/2-100,
                             SH/20);

    }else
        messageItemN->show();


    if(!messageItemN1){
        messageItemN1=new QGraphicsTextItem;
        scene()->addItem(messageItemN1);
        QString messageN1("第一关：白细胞，可用一颗尖刺消灭，碰撞减少一滴健康值");
        messageItemN1->setPlainText(messageN1);
        messageItemN1->setDefaultTextColor(Qt::black);
        messageItemN1->setFont(QFont("Courier New",16,QFont::Bold));

        messageItemN1->setPos(SW/2-300,
                              SH/20+52);

    }else
        messageItemN1->show();

    if(!messageItemN10){
        messageItemN10=new QGraphicsTextItem;
        scene()->addItem(messageItemN10);
        QString messageN10("（白细胞：活跃在细胞免疫最前线）");
        messageItemN10->setPlainText(messageN10);
        messageItemN10->setDefaultTextColor(Qt::black);
        messageItemN10->setFont(QFont("Courier New",16,QFont::Bold));

        messageItemN10->setPos(SW/2-250,
                               SH/20+52+20);

    }else
        messageItemN10->show();

    if(!messageItemN2){
        messageItemN2=new QGraphicsTextItem;
        scene()->addItem(messageItemN2);
        QString messageN2("第二关：红细胞，可用一颗尖刺消灭并增加一滴健康值");
        messageItemN2->setPlainText(messageN2);
        messageItemN2->setDefaultTextColor(Qt::black);
        messageItemN2->setFont(QFont("Courier New",16,QFont::Bold));

        messageItemN2->setPos(SW/2-300,
                              SH/20+52+40);

    }else
        messageItemN2->show();

    if(!messageItemN20){
        messageItemN20=new QGraphicsTextItem;
        scene()->addItem(messageItemN20);
        QString messageN20("（红细胞：运输氧气与养料）");
        messageItemN20->setPlainText(messageN20);
        messageItemN20->setDefaultTextColor(Qt::black);
        messageItemN20->setFont(QFont("Courier New",16,QFont::Bold));

        messageItemN20->setPos(SW/2-250,
                               SH/20+52+60);

    }else
        messageItemN20->show();

    if(!messageItemN3){
        messageItemN3=new QGraphicsTextItem;
        scene()->addItem(messageItemN3);
        QString messageN3("第三关：巨噬细胞，可用一颗尖刺消灭，碰撞减少两滴健康值");
        messageItemN3->setPlainText(messageN3);
        messageItemN3->setDefaultTextColor(Qt::black);
        messageItemN3->setFont(QFont("Courier New",16,QFont::Bold));

        messageItemN3->setPos(SW/2-300,
                              SH/20+52+80);

    }else
        messageItemN3->show();

    if(!messageItemN30){
        messageItemN30=new QGraphicsTextItem;
        scene()->addItem(messageItemN30);
        QString messageN30("（巨噬细胞：移动速度较慢，但杀伤力较大）");
        messageItemN30->setPlainText(messageN30);
        messageItemN30->setDefaultTextColor(Qt::black);
        messageItemN30->setFont(QFont("Courier New",16,QFont::Bold));

        messageItemN30->setPos(SW/2-250,
                               SH/20+52+100);

    }else
        messageItemN30->show();

    if(!messageItemN4){
        messageItemN4=new QGraphicsTextItem;
        scene()->addItem(messageItemN4);
        QString messageN4("第四关：T细胞，可用一颗尖刺消灭，移动轨迹随机变化");
        messageItemN4->setPlainText(messageN4);
        messageItemN4->setDefaultTextColor(Qt::black);
        messageItemN4->setFont(QFont("Courier New",16,QFont::Bold));

        messageItemN4->setPos(SW/2-300,
                              SH/20+52+120);

    }else
        messageItemN4->show();

    if(!messageItemN40){
        messageItemN40=new QGraphicsTextItem;
        scene()->addItem(messageItemN40);
        QString messageN40("（T细胞：免疫细胞的一种，因特异性对细菌有较大威胁）");
        messageItemN40->setPlainText(messageN40);
        messageItemN40->setDefaultTextColor(Qt::black);
        messageItemN40->setFont(QFont("Courier New",16,QFont::Bold));

        messageItemN40->setPos(SW/2-250,
                               SH/20+52+140);

    }else
        messageItemN40->show();

    if(!messageItemN5){
        messageItemN5=new QGraphicsTextItem;
        scene()->addItem(messageItemN5);
        QString messageN5("健康值归零失败，得分大于100获胜");
        messageItemN5->setPlainText(messageN5);
        messageItemN5->setDefaultTextColor(Qt::black);
        messageItemN5->setFont(QFont("Courier New",16,QFont::Bold));

        messageItemN5->setPos(SW/2-200,
                              SH/20+52+220);

    }else
        messageItemN5->show();

    if(!messageItemN50){
        messageItemN50=new QGraphicsTextItem;
        scene()->addItem(messageItemN50);
        QString messageN50("按⬆上移，按⬇下移，按空格发射尖刺");
        messageItemN50->setPlainText(messageN50);
        messageItemN50->setDefaultTextColor(Qt::black);
        messageItemN50->setFont(QFont("Courier New",16,QFont::Bold));

        messageItemN50->setPos(SW/2-200,
                               SH/20+52+240);

    }else
        messageItemN50->show();


}

//控制关卡
void Player::timerEvent(QTimerEvent *)
{
    if(playing==1){
        enemySpawn();

    }
    if(playing==2){
        enemySpawn();
        hongSpawn();
    }
    if(playing==3){
        enemySpawn();
        hongSpawn();
        jushiSpawn();
    }
    if(playing==4){
        t_cellSpawn();
        hongSpawn();
        jushiSpawn();
    }
    if(Health::getInstance().getHealth()<=0){
        gameOver();
    }
    if(Score::getInstance().getScore()>=100){
        win=1;
        gameOver();
    }


}
